/**
 * 
 */
package com.ideazd.api.controller;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



import com.ideazd.api.model.TopicPost;
import com.ideazd.api.model.TopicPostPraise;
import com.ideazd.api.model.dto.RetDTO;
import com.ideazd.api.tools.DateUtil;
import com.ideazd.api.tools.SysUtil;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.log.Logger;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.DbKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;

/**
 * @author yzz
 * @create 2016年3月23日 下午2:44:19
 */
@ControllerBind(controllerKey="post")
public class TopicPostController extends Controller {
	private static final Logger log = Logger.getLogger(TopicPostController.class);
	
	/*
	 * 话题帖子
	 */
	public void getTopicPostList(){
		String topId = getPara("topId");
		String pageIndex = getPara("pageIndex");
		String displayOrder = getPara("displayOrder");
		String displayTop = getPara("displayTop");
		String useId  = getPara("useId ");
		Map<Object, Object> ret = new HashMap<Object,Object>();
		
		if(StrKit.isBlank(pageIndex)||StrKit.isBlank(displayOrder)){
			ret.put("code","-2");
			ret.put("msg","必传参数不允许为空");
			renderJson(ret);
			return;
		}
		
		String topIdStr = "";
		
		if(StrKit.isBlank(topId)){
			topIdStr = "and p.topid = '"+topId+"'";
		}
		
		String top = "";
		
		if(!StrKit.isBlank(displayTop)){
			top = "displaytop ,";
		}
		
		String sort = "createtime";
		
		if(displayOrder.equals("2")){
			sort = "clickamount";
		}
		
		String userId = "";
		
		if(!StrKit.isBlank(useId)){
			userId = "and p.useid = '"+useId+"'";
		}
		
		String sql = "from sta_topic_post p left join sta_user u on u.useid = p.useid  where 1=1 "+topIdStr+" "+userId+" order by "+top+sort+"";
		
		try {
			
			Page<TopicPost> topicPost = TopicPost.dao.paginate(Integer.parseInt(pageIndex), Integer.parseInt(PropKit.get("firstLimit")), "select p.*,u.nickname,u.headportrait ", sql);
			List<Map<Object, Object>> topicList = new ArrayList<Map<Object, Object>>();
			
			for(TopicPost post : topicPost.getList()){
				Map<Object, Object> topic = new HashMap<Object,Object>();
				topic.put("posId", post.get("posid"));
				topic.put("nickName", post.get("nickname"));
				topic.put("headPortrait", post.get("headportrait"));
				topic.put("posContent", post.get("poscontent"));
				topic.put("posPicture", PropKit.get("server_uri") +  post.get("pospicture"));
				topic.put("createTime", SysUtil.getPublishDate(post.get("createtime").toString()));
				topic.put("clickAmount", post.get("clickamount"));
				topic.put("commentAmount", post.get("commentamount"));
				topicList.add(topic);
			}
			
			Map<Object, Object> data = new HashMap<Object,Object>();
			data.put("totalPage", topicPost.getTotalPage());
			data.put("topicPostList", topicList);
			
			ret.put("code","0");
			ret.put("msg","成功");
			ret.put("data", data);
			
		} catch (Exception e) {
			ret.put("code","-1");
			ret.put("msg",e.getMessage());
		}
		
		renderJson(ret);
		return;
		
		
	}
	
	/**
	 * 发布帖子
	 */
	public void publishTopicPost(){
		String topId = getPara("topId");
		String useId = getPara("useId");
		String posContent = getPara("posContent");
		String posPicture = getPara("posPicture");
		
        RetDTO ret = new RetDTO();
		
		if(StrKit.isBlank(topId)||StrKit.isBlank(useId)||StrKit.isBlank(posContent)){
			ret.setCode("-2");
			ret.setMsg("必传参数不允许为空");
			renderJson(ret);
			return;
		}
		
		try {
			
			String uuid = SysUtil.getUUID();
			
			//图片上传
			if(!StrKit.isBlank(posPicture)){
				
                Map<String, String> imaPath = SysUtil.uploadImg(getRequest(),posPicture,"/upload/topicpost"+"/"+uuid+"/images"+"/"+DateUtil.formatDate(new Date(),"yyyyMMdd")+"/");
				
                posPicture = imaPath.get("diskPath");
				
			}
			
			
			
			
			TopicPost topicPost = new TopicPost();
			topicPost.set("posid", uuid);
			topicPost.set("topid", topId);
			topicPost.set("useid", useId);
			topicPost.set("poscontent", posContent);
			topicPost.set("pospicture", posPicture);
			topicPost.set("createtime", new Date());
			topicPost.set("clickamount", 0);
			topicPost.set("commentamount", 0);
			topicPost.set("displaytop", 0);
			topicPost.set("delflag", 0);
			topicPost.set("unread", 0);
			topicPost.save();
			
			ret.setCode("0");
			ret.setMsg("成功");
			
		} catch (Exception e) {
			ret.setCode("-1");
			ret.setMsg(e.getMessage());
		}
		
		renderJson(ret);
		return;
		
	}
	
	/**
	 * 帖子详情
	 */
	public void getTopicPostDetail(){
		String posId = getPara("posId");
		String useId = getPara("useId");
		
        RetDTO ret = new RetDTO();
		
		if(StrKit.isBlank(posId) || StrKit.isBlank(useId)){
			ret.setCode("-2");
			ret.setMsg("必传参数不允许为空");
			renderJson(ret);
			return;
		}
		
		try {
			
			TopicPost topicPost = TopicPost.dao.findFirst("SELECT p.*,t.`toptitle`,u.`nickname`,u.`headportrait` FROM sta_topic_post p LEFT JOIN sta_topic t ON p.`topid` = t.`topid` LEFT JOIN sta_user u ON u.`useid` = p.`useid` WHERE p.`posid` = '"+posId+"'");
			
			if(topicPost.get("useid").equals(useId)){
				TopicPost post = new TopicPost();
				post.set("posid", topicPost.get("posid"));
				post.set("unread", 0);
				post.update();
			}
			
			ret.put("topTitle", topicPost.get("toptitle"));
			ret.put("posContent", topicPost.get("poscontent"));
			ret.put("posPicture", PropKit.get("server_uri") + topicPost.get("pospicture"));
			ret.put("nickName", topicPost.get("nickname"));
			ret.put("headPortrait", topicPost.get("headportrait"));
			ret.put("createTime", topicPost.get("createtime"));
			ret.put("clickAmount", topicPost.get("clickamount"));
			
			ret.setCode("0");
			ret.setMsg("成功");
			
		} catch (Exception e) {
			ret.setCode("-1");
			ret.setMsg(e.getMessage());
		}
		
		renderJson(ret);
		return;
	}
	
	/**
	 * 帖子点赞
	 */
	@Before(Tx.class)
	public void clickLikePost(){
		String posId = getPara("posId");
		String useId = getPara("useId");
		
        RetDTO ret = new RetDTO();
		
		if(StrKit.isBlank(posId)||StrKit.isBlank(useId)){
			ret.setCode("-2");
			ret.setMsg("必传参数不允许为空");
			renderJson(ret);
			return;
		}
		
		try {
			
			TopicPost p = TopicPost.dao.findById(posId);
			TopicPostPraise  tpp = new TopicPostPraise();
			tpp.set("praid", SysUtil.getUUID());
			tpp.set("topid", p.get("topid"));
			tpp.set("posid", posId);
			tpp.set("useid", useId);
			tpp.set("createtime", new Date());
			tpp.save();
			
			Db.update("update sta_topic_post clickamount = clickamount+1 where posid = '"+posId+"'");
			
			
			ret.setCode("0");
			ret.setMsg("成功");
			
		} catch (Exception e) {
			ret.setCode("-1");
			ret.setMsg(e.getMessage());
			try {
				DbKit.getConfig().getConnection().rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
		}
		
		renderJson(ret);
		return;
	}
}
